package org.lq.ht.view.dictionary;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.MutableTreeNode;
import javax.swing.tree.TreePath;

import org.lq.ht.dao.impl.GoodsDaoImpl;
import org.lq.ht.entity.Dictionary;
import org.lq.ht.entity.Employee;
import org.lq.ht.entity.Goods;
import org.lq.ht.entity.Room;
import org.lq.ht.entity.TypeID;
import org.lq.ht.service.DictionaryService;
import org.lq.ht.service.EmployeeService;
import org.lq.ht.service.GoodsService;
import org.lq.ht.service.RoomService;
import org.lq.ht.service.impl.DictionaryServiceImpl;
import org.lq.ht.service.impl.EmployeeServiceImpl;
import org.lq.ht.service.impl.GoodsServiceImpl;
import org.lq.ht.service.impl.RoomServiceImpl;
import org.lq.ht.view.dictionary.V_dictionary;
import org.lq.ht.util.CastUtil;
import org.lq.ht.view.dictionary.V_dictionary;

import javax.swing.JTabbedPane;
import javax.swing.JMenuBar;
import javax.swing.JOptionPane;
import javax.swing.JButton;
import javax.swing.JTree;
import javax.swing.JScrollPane;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class V_dictionary extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private DictionaryService dictService = new DictionaryServiceImpl();
	private JPanel contentPane;
	private JTree tree_room;
	private JTree tree_goodType;
	private JTree tree_util;
	private JTree tree_post;


	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					V_dictionary frame = new V_dictionary();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public V_dictionary() {
		setTitle("数据字典");
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 389, 444);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);

		JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
		contentPane.add(tabbedPane, BorderLayout.CENTER);
		
		JPanel p_roomType = new JPanel();
		tabbedPane.addTab("房间类型", null, p_roomType, null);
		p_roomType.setLayout(new BorderLayout(0, 0));
		
		tree_room = new JTree();
		p_roomType.add(tree_room, BorderLayout.CENTER);
		
		JPanel p_goodType = new JPanel();
		tabbedPane.addTab("商品类型", null, p_goodType, null);
		p_goodType.setLayout(new BorderLayout(0, 0));
		
		tree_goodType = new JTree();
		p_goodType.add(tree_goodType, BorderLayout.CENTER);
		
		JPanel p_unit = new JPanel();
		tabbedPane.addTab("单位", null, p_unit, null);
		p_unit.setLayout(new BorderLayout(0, 0));
		
		tree_util = new JTree();
		p_unit.add(tree_util, BorderLayout.CENTER);
		
		JPanel p_position = new JPanel();
		tabbedPane.addTab("职务", null, p_position, null);
		p_position.setLayout(new BorderLayout(0, 0));
		
		tree_post = new JTree();
		p_position.add(tree_post, BorderLayout.CENTER);
		
		JPanel p_btns = new JPanel();
		FlowLayout flowLayout = (FlowLayout) p_btns.getLayout();
		flowLayout.setHgap(20);
		contentPane.add(p_btns, BorderLayout.SOUTH);
		
		JButton button_new = new JButton("新建类别");
		button_new.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				//根据获取的选项卡判断添加的值内容
				int tabIndex = tabbedPane.getSelectedIndex();
				Dictionary d = new Dictionary();
				List<Dictionary> list = null;
				String title = "";
				
				switch (tabIndex) {
				case 0:
					title = "房间类型";
					d.setType_id(TypeID.房间类型);
					list = dictService.findById(0);
					break;
				case 1:
					title = "商品类型";
					d.setType_id(TypeID.商品类型);
					list = dictService.findById(1);
					break;
				case 2:
					title = "单位";
					d.setType_id(TypeID.单位);
					list = dictService.findById(3);
					break;
				case 3:
					title = "职位";
					d.setType_id(TypeID.职位);
					list = dictService.findById(4);
					break;
				}
				String value = JOptionPane.showInputDialog(V_dictionary.this, "请输入"+title+"的名称:",title,JOptionPane.PLAIN_MESSAGE);
				boolean flag = true;
				for (Dictionary dic : list) {
					if (!(dic.getName().equals(value))) {
						flag = true;
					}else {
						flag = false;
					}
					}
				if(value!=null && !"".equals(value)) {//不等于空,并且输入的不空
					if (flag == false) {
						JOptionPane.showMessageDialog(V_dictionary.this, "已经有此类型,无需重复添加!");
					}else if(flag == true){
						d.setName(value);
						showMessage(dictService.saveDictionary(d), "添加");
					}
					}
			}
		});
		button_new.setBounds(69, 10, 97, 23);
		p_btns.add(button_new);
		
		JButton button_rem = new JButton("重命名");
		button_rem.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				JTree tree = null;
				int tabIndex = tabbedPane.getSelectedIndex();
				String title = "";
				List<Dictionary> upList = new ArrayList<>();
				switch (tabIndex) {
				case 0:
					title = "房间类型";
					tree = tree_room;
					upList = dictService.findById(0);
					break;
				case 1:
					title = "商品类型";
					tree = tree_goodType;
					upList = dictService.findById(1);
					break;
				case 2:
					title = "单位";
					tree = tree_util;
					upList = dictService.findById(3);
					break;
				case 3:
					title = "职位";
					tree = tree_post;
					upList = dictService.findById(4);
					break;
				}
				if(tree.getSelectionPath()!=null) {
					//获取当前选中的节点的值
					String selectValue =  CastUtil.castString(tree.getSelectionPath().getLastPathComponent());
					Object value =JOptionPane.showInputDialog(V_dictionary.this, "请输入"+title+"的名称:",title,JOptionPane.PLAIN_MESSAGE,null,null,selectValue);
					boolean bool = true;
					for (Dictionary dic : upList) {
						if (dic.getName().equals(value)) {
							bool = false;
						}else {
							bool = true;
						}
						}
					if(value!=null && !"".equals(value)) {
						//获取选中的节点
						DefaultMutableTreeNode dmtn = (DefaultMutableTreeNode) tree.getSelectionPath().getLastPathComponent();
						Dictionary d= (Dictionary) dmtn.getUserObject();
						if (bool) {
							d.setName(CastUtil.castString(value));
							System.out.println(d.getNo());
							showMessage(dictService.updateDictionary(d), "修改");
						}else {
							JOptionPane.showMessageDialog(V_dictionary.this, "亲,内容不能重复哦!");
						}
					}
				}else {
					showMessage(false, "请选择修改的节点,修改");
				}
				
			}
		});
		button_rem.setBounds(244, 10, 97, 23);
		p_btns.add(button_rem);
		
		JButton button_del = new JButton("删除类别");
		button_del.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				JTree tree = null;
				int tabIndex = tabbedPane.getSelectedIndex();
				switch (tabIndex) {
				case 0:
					tree = tree_room;
					break;
				case 1:
					tree = tree_goodType;
					break;
				case 2:
					tree = tree_util;
					break;
				case 3:
					tree = tree_post;
					break;
				}
				//	获取树节点的数据模型
				DefaultTreeModel dtm = (DefaultTreeModel) tree.getModel();
				//通过节点的数据模型,删除选中的节点对象
				TreePath selectionPath = tree.getSelectionPath();
				if(selectionPath!=null) {
					//获取删除节点对象的最后一节点
					MutableTreeNode mtn = (MutableTreeNode) selectionPath.getLastPathComponent();
					//询问是否删除
					int n = JOptionPane.showConfirmDialog(V_dictionary.this, "你确定删除吗?","删除",JOptionPane.OK_CANCEL_OPTION);
					if(n==0) {
						//删除根据编号删除
						DefaultMutableTreeNode dmtn = (DefaultMutableTreeNode) mtn;
						//获取添加到节点中的对象,节点存储的是什么,获取的就是什么
						Dictionary dict = (Dictionary) dmtn.getUserObject();//被选中的字典对象
						String name = dict.getName();
						
						List goodType = dictService.isType();
						List goodUnit = dictService.isUnit();
						List roomType = dictService.isRoomType();
						
						boolean goodsTypeBool = false;
						for (Object object : goodType){
							if (((dictService.getById((int)object)).getName()).equals(name)) {
								System.out.println("商品类型名称:"+(dictService.getById((int)object).getName()));
								goodsTypeBool = true;
								break;
							}else {
								System.out.println("=====284======");
								goodsTypeBool = false;
							}
							
						}
						
						
						boolean goodsUnitBool = false;
						for (Object object : goodUnit) {
							if (((dictService.getById((int)object)).getName()).equals(name)) {
								System.out.println("商品单位名称:"+((dictService.getById((int)object)).getName()).equals(name));
								goodsUnitBool = true;
								break;
							}else {
								System.out.println("=======296========");
								goodsUnitBool = false;
							}
						}
						//判断选中的节点  是否有此类型的数据
						boolean roomTypeBool = false;
						
						for (Object object : roomType) {
							
							if (((dictService.getById(CastUtil.castInt(object))).getName()).equals(name)) {
								System.out.println("房间类型名称:"+((dictService.getById(CastUtil.castInt(object))).getName()).equals(name));
								roomTypeBool = true;
								break;
							}else {
								System.out.println("======308========");
								roomTypeBool = false;
							}
						}
						
//						boolean goodsUnitBool = dictService.isUnit()==null ? false : true;
//						boolean roomTypeBool = dictService.isRoomType()==null ? false : true;
//						RoomService rs = new RoomServiceImpl();
//						List<Room> roomList = rs.findAll();
//						boolean roomBool = false;
//						for (Room room : roomList) {
//							if ((CastUtil.castString(room.getType())).equals(name)) {
//								roomBool = false;
//							}
//							else {
//								roomBool = true;
//							}
//						}
						
						EmployeeService es = new EmployeeServiceImpl();
						List<Employee> employeeList = es.findAll();
						boolean empBool = false;
						for (Employee employee : employeeList) {
							if (employee.getPosition().equals(name)) {
								System.out.println(employee.getPosition());
								empBool = true;
								break;
							}else {
								System.out.println("======332=====");
								empBool = false;
							}
						}
						
						if (goodsTypeBool) {
							JOptionPane.showMessageDialog(V_dictionary.this, "此数据正在使用,不能删除");
						}if (goodsUnitBool) {
							JOptionPane.showMessageDialog(V_dictionary.this, "此数据正在使用,不能删除");
						}if (roomTypeBool) {
							JOptionPane.showMessageDialog(V_dictionary.this, "此数据正在使用,不能删除");
						} if (empBool) {
							JOptionPane.showMessageDialog(V_dictionary.this, "此数据正在使用,不能删除");
						}
						if(goodsTypeBool==false&&goodsUnitBool==false&&roomTypeBool==false&&empBool==false) {
							showMessage(dictService.deleteDictionaryById(dict.getNo()), "删除");
							//删除节点
							dtm.removeNodeFromParent(mtn);
						}
					}
				}else {
					//未选中节点
					JOptionPane.showMessageDialog(V_dictionary.this, "请选择节点后删除!");
				}

			}
		});
		button_del.setBounds(407, 10, 97, 23);
		p_btns.add(button_del);
		
		initTree();
	}
	
	
	
	/**
	 * 消息提示
	 * @param bool
	 * @param message
	 */
	public void showMessage(boolean bool,String message) {
		if (bool) {
			JOptionPane.showMessageDialog(V_dictionary.this, message+"成功",message,JOptionPane.INFORMATION_MESSAGE);
			initTree();
		}else {
			JOptionPane.showMessageDialog(V_dictionary.this, message+"失败",message,JOptionPane.ERROR_MESSAGE);
		}
	}
//	/**
//	 * 初始化房间类型的方法
//	 */
//	public void initRoomTree() {
//		List<Dictionary> roomList = dictService.findById(TypeID.商品类型);
//		DefaultMutableTreeNode dmtn = new DefaultMutableTreeNode();
//		for (Dictionary room : roomList) {
//			dmtn.add(new DefaultMutableTreeNode(room));
//		}
//		DefaultTreeModel dtm = new DefaultTreeModel(dmtn);
//		tree_room.setModel(dtm);
//	}
	/**
	 * 初始化四棵树的方法
	 */
	public void initTree() {
		init(TypeID.商品类型,"商品类型节点",tree_goodType);
		init(TypeID.房间类型,"房间类型节点",tree_room);
		init(TypeID.单位,"单位节点",tree_util);
		init(TypeID.职位,"职位节点",tree_post);
	}
	/**
	 * 初始化通用方法
	 * @param t
	 */
	public void init(TypeID t,String str,JTree vd) {
		List<Dictionary> list = dictService.findById(t);
		DefaultMutableTreeNode dmtn = new DefaultMutableTreeNode(str);
		for (Dictionary d : list) {
			dmtn.add(new DefaultMutableTreeNode(d));
		}
		DefaultTreeModel dtm = new DefaultTreeModel(dmtn);
		vd.setModel(dtm);
	}
}
